export class Point {
    /* 增删改查 */
    /**
     *@param {Array} position = [114.30,30.50] 
     *@param {Array} attr = [{name:事件编号,type:"string",value: inputId}]
     *@param {Object} service = {name:"xm",layerId:2} 
     *@param {Object} docLayer = $docLayer
     *  
     * */
    static add({
        position,
        attr,
        service,
        docLayer
    }) {
        /* new ol.geom.Point([118,30]) */
        var gpoint = new Zondy.Object.GPoint(position[0], position[1]);//createPoint();
        //设置当前点要素的几何信息
        var fGeom = new Zondy.Object.FeatureGeometry({ PntGeom: [gpoint] });
        // 根据事件状态设置点要素颜色
        let color;
        if (attr.filter(item => item.name === "处理状态").map(item => item.value) == 0) {
            color = 6;
        }
        else if (attr.filter(item => item.name === "处理状态").map(item => item.value) == 1) {
            color = 4;
        }
        else if (attr.filter(item => item.name === "处理状态").map(item => item.value) == 2) {
            color = 90;
        };
        //描述点要素的符号参数信息
        var pointInfo = new Zondy.Object.CPointInfo({
            Angle: 0,
            Color: color,//子图颜色
            Space: 0,
            SymHeight: 5,//高度
            SymID: 21,//子图号
            SymWidth: 5//宽度
        });
        //设置当前点要素的图形参数信息
        var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
            /* 1-Point  2-Line  3-Area */
            InfoType: 1,
            PntInfo: pointInfo
        });
        var attValue = attr.map(item => item.value)
        //创建一个要素
        var feature = new Zondy.Object.Feature({
            fGeom: fGeom,//几何信息
            GraphicInfo: webGraphicInfo,//图形信息
            AttValue: attValue//属性信息
        });
        //设置要素为点要素
        feature.setFType(1);//1 -point 2--line 3-area
        /* 2、创建数据源 */
        /* new ol.source.Vector({}) */
        var featureSet = new Zondy.Object.FeatureSet();
        //设置属性结构  数据源的属性结构和mapgis中的图层的字段映射
        var cAttStruct = new Zondy.Object.CAttStruct({
            FldName: attr.map(item => item.name),
            FldNumber: attr.length,
            FldType: attr.map(item => item.type),
        });
        featureSet.AttStruct = cAttStruct;
        //添加要素到要素数据集
        featureSet.addFeature(feature);
        /* 3、调用服务向mapgis中的地图文档中的图层添加 */
        /* 地图文档的名称 Map
           添加要素所在的图层 Layer
         */
        /* http请求  post */
        var editService = new Zondy.Service.EditDocFeature(service.name, service.layerId, {
        });
        //执行添加点要素功能
        editService.add(featureSet, this.onPntSuccess(docLayer));
    }
    /* update attr必选项*/
    static update({
        position = [],
        attr = [],
        fid,
        service,
        docLayer,
    }) {
        let fGeom = null;
        if (position.length) {
            var gpoint = new Zondy.Object.GPoint(position[0], position[1]);
            fGeom = new Zondy.Object.FeatureGeometry({ PntGeom: [gpoint] });
        }
        //设置添加点要素的图形参数信息
        let webGraphicInfo = null;
        // 根据事件状态设置点要素颜色
        let color;
        if (attr.filter(item => item.name === "处理状态").map(item => item.value) == 0) {
            color = 6;
        }
        else if (attr.filter(item => item.name === "处理状态").map(item => item.value) == 1) {
            color = 4;
        }
        else if (attr.filter(item => item.name === "处理状态").map(item => item.value) == 2) {
            color = 90;
        };
        if (color) {
            var pointInfo = new Zondy.Object.CPointInfo({
                Angle: 0,
                Color: color,//子图颜色
                Space: 0,
                SymHeight: 5,//高度
                SymID: 21,//子图号
                SymWidth: 5//宽度
            });
            //设置当前点要素的图形参数信息
            webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
                InfoType: 1,
                PntInfo: pointInfo
            });
        }


        //设置添加点要素的属性信息
        var attValue = attr.map(item => item.value);


        //创建一个点要素
        var newFeature = new Zondy.Object.Feature({
            fGeom: fGeom,
            GraphicInfo: webGraphicInfo,
            AttValue: attValue
        });
        //设置要素为点要素
        newFeature.setFType(1);
        newFeature.setFID(fid);
        /* 设置要素集 */
        var featureSet = new Zondy.Object.FeatureSet();
        //设置属性结构
        var cAttStruct = new Zondy.Object.CAttStruct({
            FldName: attr.map(item => item.name),
            FldNumber: attr.length,
            FldType: attr.map(item => item.type)
        });
        featureSet.AttStruct = cAttStruct;
        //添加要素到要素数据集
        featureSet.addFeature(newFeature);
        //创建一个编辑服务类
        var editService = new Zondy.Service.EditDocFeature(service.name, service.layerId, {

        });
        editService.update(featureSet, this.onPntSuccess(docLayer));
    }
    static onPntSuccess(docLayer) {
        return function (data) {
            console.log(data)
            if (data.succeed) {
                // alert("操作要素成功！");
                ElMessage({
                    message: '操作要素成功！',
                    type: 'success',
                  })
                docLayer.refresh();
            } else {
                // alert("操作要素失败！");
                ElMessage.error('操作要素失败！')
            }
        }
    }
}